auto.waitFor();
device.wakeUpIfNeeded();
const {
    pwd
} = hamibot.env;

const WIDTH = Math.min(device.width, device.height);
const HEIGHT = Math.max(device.width, device.height);

var km = context.getSystemService(context.KEYGUARD_SERVICE);
if (!(km.inKeyguardRestrictedInputMode() && km.isKeyguardSecure())) {
    // 不需要解锁
    toast("不需要解锁");
} else {
    toast("当前需要解锁，正在自动解锁！")
    let x = WIDTH / 2;
    let y = HEIGHT - 300;
    sleep(2000);
    sml_move(x, y, x, HEIGHT / 2, 1000);
    sleep(1500);
    if (pwd) {
        for (let i = 0; i < pwd.length; i++) {
            click(pwd[i])
        }
    }
}

// com.tencent.mm.app.WeChatSplashActivity 启动页
// com.tencent.mm.ui.LauncherUI 微信主页
// com.tencent.mm.plugin.webview.ui.tools.WebviewMpUI 微信网页
let delay_time = 8000;
sleep(3000);
if (!launchPackage("com.tencent.mm")) {
    toast("启动微信失败！");
    exit();
}
waitForPackage("com.tencent.mm", 1000);
toast("启动微信成功！");
while (currentPackage() == "com.tencent.mm") {
    switch (currentActivity()) {
        case "com.tencent.mm.app.WeChatSplashActivity":
            toast("启动页,等待进入微信主页");
            sleep(1000);
            break;
        case "com.tencent.mm.ui.LauncherUI":
            toast("当前页面为微信主页，准备滑动进入浮窗页面");
            sleep(2000);
            sml_move(100, 1000, 1000, 1000, 1000);
            break;
        case "com.tencent.mm.plugin.webview.ui.tools.WebviewMpUI":
            toast("当前页面为微信浮窗页面，检测页面内容");
            sleep(delay_time);
            if (text("快鸭网址导航手机版").exists()) {
                toast("当前为快鸭网址导航手机版页面，正在进入阅读页面");
                myClick("广告");
                sleep(delay_time);
            }
            task();
            exit();
    }
}

function task() {
    className("android.view.View").text("开始阅读").waitFor();
    toast("检测是否符合阅读条件！");
    sleep(1000);
    if (checkEnd(true)) {
        endEnd();
        return;
    }
    toast("符合阅读条件！");
    let isOk = false;
    for (let i = 0; i < 3; i++) {
        sleep(1000);
        myClick("开始阅读");
        if (!checkDialog()) {
            isOk = true;
            break;
        }
    }
    if (!isOk) {
        toast("阅读异常！");
    }
    sleep(1000);
    while (1) {
        startReadTask();
        sleep(random(1000, 2000));
        if (checkEnd(false)) {
            endEnd();
        }
    }
}

function myClick(inputText) {
    let b = text(inputText).findOne().bounds();
    click(b.centerX(), b.centerY())
}

function checkEnd(isFirstTry) {
    if (text("本小时已达到阅读限制，请勿多个平台阅读").exists()) {
        toast("本小时已达到阅读限制，请勿多个平台阅读")
        return true;
    }
    // let nextTaskText = textContains("下一批文章将在").exists() ? textContains("下一批文章将在").findOne().text() : null;
    if (textContains("下一批文章将在").exists()) {
        if (isFirstTry) {
            toast("检测当前页面是否有刷新，正在刷新重试！");
            back();
            sleep(3000);
            return checkEnd(false);
        } else {
            toast("休息时间~");
            return true;
        }
    }
    if (text("今天已达到阅读限制，请勿多个平台阅读").exists()) {
        toast("今天已达到阅读限制，请勿多个平台阅读")
        withdraw();
        return true;
    }
    return false;
}

// 检测是否有弹窗，有的话就点击确定
function checkDialog() {
    sleep(1000);
    if (text("确定").exists()) {
        text("确定").click();
        return true;
    }
    return false;
}

function endEnd() {
    toast("任务结束！正在退出！");
    sleep(random(1000, 2000));
    back();
    sleep(random(1000, 2000));
    back();
    sleep(random(1000, 2000));
    back();
    sleep(random(1000, 2000));
    home();
    exit();
}

// 提现
function withdraw() {
    text("积分兑换").click();
    toast("点击积分兑换");
    text("提现").waitFor();
    toast("进入到提现页面");
    sleep(1000);
    if (!checkDialog()) {
        text("提现").click();
        toast("点击提现");
        back();
    }

}


function startReadTask() {
    for (let i = 0; i < random(6, 10); i++) {
        sml_move(random(300, 500), random(1500, 1800), random(700, 800), random(200, 300), random(1000, 2000));
        sleep(random(1000, 2000));
    }

    if (random(1, 10) <= 3) {
        textStartsWith("赞").click()
        toast("随机三成概率点赞")
    }

    sleep(random(1000, 2000));
    back();
    sleep(1000);
    checkDialog();
}

//此代码由飞云脚本圈整理提供（www.feiyunjs.com）
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};

//仿真随机带曲线滑动  
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    };

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    gesture.apply(null, xxy);
}